<?php


namespace app\merchant\controller;

use other\MessageSend;
use think\Db;
use think\Log;

class Message extends Base
{
    private $msg_type=[
        1=>'系统消息',
        2=>'右下角弹窗',
        3=>'居中强弹窗',
        4=>'短信消息',
    ];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $info = db('merchant')->where('id',session('aid'))->find();
        if($info['status']!=1){
            session('aid',null);
            cookie('aid',null);
            $this->redirect(url('Login/login'));
        }

        $this->assign('info',$info);

        return $this->fetch('',[
            'msglist' => $this->msg_type,
        ]);
    }


    public function lists()
    {
        // 查询条件start
        $msg_type = input('msg_type','');
        $times = input('times','');
        $status = input('status','');
        $page = input('page','');
        $limit = input('limit','');

        $map['role_type'] = 1;


        $map['a_id']=$this->uid;



        if(!empty($msg_type)){
            $map['msg_type']=$msg_type;
        }
        if(!empty($phone)){
            $aId=model('Merchant')->where('phone',$phone)->value('id');
            if($aId){
                $map['a_id']=$aId;
            }else{
                $map['a_id']=0;
            }
        }
        if(!empty($username)){
            $aId=model('Merchant')->where('username',$username)->value('id');
            if($aId){
                $map['a_id']=$aId;
            }else{
                $map['a_id']=0;
            }
        }
        if(!empty($mer_name)){
            $aId=model('Merchant')->where('mer_name',$mer_name)->value('id');
            if($aId){
                $map['a_id']=$aId;
            }else{
                $map['a_id']=0;
            }
        }
        if(!empty($mer_desc)){
            $aId=model('Merchant')->where('mer_desc',$mer_desc)->value('id');
            if($aId){
                $map['a_id']=$aId;
            }else{
                $map['a_id']=0;
            }
        }


        if($status!=''){
            $map['a.status']=$status;
        }

        if($times != ''){
            $time_arr = explode(' - ',$times);
            $map['a.create_time'] = ['between',[strtotime($time_arr[0]),strtotime($time_arr[1])]];
        }

        $infos = model('MsgNotice')->alias('a')->field('a.*')
            ->where($map)->order('a.id desc')->page($page,$limit)
            ->select();

        $total= model('MsgNotice')->alias('a')
            ->where($map)->count();

        foreach ($infos as &$v){
            $v['msg_type']=$this->msg_type[$v['msg_type']];
        }

        return json([
            'code'=>200,
            'data' => $infos,
            'total' => $total,
        ]);
    }

    public function status()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('MsgNotice')->save(['status'=>input('post.status')],['id'=>$id,'a_id'=>$this->uid]);
            if($res){
                return json(['code'=>200,'msg'=>'已读成功']);
            }else{
                return json(['code'=>400,'msg'=>'已读失败']);
            }
        }else{
            return json(['code'=>400,'msg'=>'已读失败']);
        }
    }

    public function statusAll()
    {
        if($this->request->isAjax()){
            $id = input('post.id/a');
            $res = model('MsgNotice')->whereIn('id',$id)->where('status',0)->where('a_id',$this->uid)->update(['status'=>1,'update_time'=>time()]);
            if($res){
                return json(['code'=>200,'msg'=>'已读成功']);
            }else{
                return json(['code'=>400,'msg'=>'已读失败']);
            }
        }else{
            return json(['code'=>400,'msg'=>'已读失败']);
        }
    }

    public function delete()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $model = 'MsgNotice';

            $info=model($model)->where('id',$id)->find();
            if(empty($info)||$info['a_id']!=$this->uid){
                return json(['code'=>400,'msg'=>'删除失败']);
            }

            $res = model($model)->destroy($id);
            if($res){

                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>400,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>400,'msg'=>'删除失败']);
        }
    }

    public function deleteAll()
    {
        if($this->request->isAjax()){
            $id = input('post.id/a');
            $res = model('MsgNotice')->whereIn('id',$id)->where('a_id',$this->uid)->update(['delete_time'=>time()]);
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>400,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>400,'msg'=>'删除失败']);
        }
    }
}